import mill._, scalalib._
import mill.modules.Assembly._

import coursier.maven.MavenRepository

object Common extends ScalaModule {
  override def repositoriesTask = T.task { super.repositoriesTask() ++ Seq(
    MavenRepository("https://mirrors.huaweicloud.com/repository/ivy/")
  ) }

  def scalaVersion = "3.1.0"

  val akkaVersion = "2.6.15"

  object test extends Tests with TestModule.Utest {
    override def ivyDeps = Agg(
      // test
      ivy"org.scalactic::scalactic:3.2.9",
      ivy"org.scalatest::scalatest:3.2.9"
    )
  }
}


object RaspPi extends ScalaModule {
  override def repositoriesTask = T.task { super.repositoriesTask() ++ Seq(
    MavenRepository("https://mirrors.huaweicloud.com/repository/ivy/")
  ) }

  def scalaVersion = "3.1.0"

  override def scalacOptions = Seq("-unchecked", "-deprecation", "-encoding", "utf8", "-feature")

  override def moduleDeps = Seq(Common)

  val akkaVersion = "2.6.15"
  val akkaDep = List(
    ivy"com.typesafe.akka:akka-actor-typed_2.13:${akkaVersion}",
    ivy"com.typesafe.akka:akka-actor-testkit-typed_2.13:${akkaVersion}",
    ivy"com.typesafe.akka:akka-stream_2.13:${akkaVersion}",
    ivy"com.typesafe.akka:akka-cluster-typed_2.13:${akkaVersion}",
    ivy"com.typesafe.akka:akka-http_2.13:10.2.6",
  )

  override def ivyDeps = Agg(
    ivy"com.pi4j:pi4j-core:1.4",
    // json
    ivy"io.circe::circe-core:0.14.1",
    ivy"io.circe::circe-generic:0.14.1",
    ivy"io.circe::circe-parser:0.14.1",
    // log
    ivy"ch.qos.logback:logback-classic:1.2.3",
    ivy"com.typesafe.scala-logging::scala-logging:3.9.4",
    // mqtt
    ivy"com.lightbend.akka:akka-stream-alpakka-mqtt_2.13:3.0.2",
    // test
    ivy"org.scalactic::scalactic:3.2.9",
    ivy"org.scalatest::scalatest:3.2.9"
  ) ++ akkaDep

  override def mainClass = Some("MainPi")

  override def assemblyRules = Seq(
    Rule.Append("application.conf"),
    //Rule.ExcludePattern(".*\\.conf"),
    Rule.Append("akka.loggers"),
    Rule.Append("reference.conf"),
    Rule.ExcludePattern("ECLIPSE_.SF"),
    Rule.ExcludePattern("ECLIPSE_.RSA")
  )

  def upload() = T.command {
    import sys.process._
    "scp ./out/RaspPi/assembly/dest/out.jar pi2:pi.jar".!
    println("upload success.")
  }

  object test extends Tests with TestModule.Utest {
    override def ivyDeps = Agg(
      // test
      ivy"org.scalactic::scalactic:3.2.9",
      ivy"org.scalatest::scalatest:3.2.9"
    )
  }
}


object Master extends ScalaModule {
  override def repositoriesTask = T.task { super.repositoriesTask() ++ Seq(
    MavenRepository("https://mirrors.huaweicloud.com/repository/ivy/")
  ) }

  override def mainClass = Some("MainAkka")

  def scalaVersion = "3.1.0"
  override def scalacOptions = Seq("-unchecked", "-deprecation", "-encoding", "utf8", "-feature")

  // for import javafx
  override def resolutionCustomizer = T.task {
    Some((_: coursier.core.Resolution).withOsInfo(coursier.core.Activation.Os.fromProperties(sys.props.toMap)))
  }

  override def moduleDeps = Seq(Common)

  val akkaVersion = "2.6.15"
  val akkaDep = List(
    ivy"com.typesafe.akka:akka-actor-typed_2.13:${akkaVersion}",
    ivy"com.typesafe.akka:akka-actor-testkit-typed_2.13:${akkaVersion}",
    ivy"com.typesafe.akka:akka-stream_2.13:${akkaVersion}",
    ivy"com.typesafe.akka:akka-cluster-typed_2.13:${akkaVersion}",
    ivy"com.typesafe.akka:akka-http_2.13:10.2.6",
  )

  val jfxVersion = "16"
  val jfxClassifier = "linux"
  val jfxDep = List(
    ivy"org.openjfx:javafx-base:${jfxVersion};classifier=${jfxClassifier}",
    ivy"org.openjfx:javafx-controls:${jfxVersion};classifier=${jfxClassifier}",
    ivy"org.openjfx:javafx-graphics:${jfxVersion};classifier=${jfxClassifier}",
    ivy"org.openjfx:javafx-media:${jfxVersion};classifier=${jfxClassifier}",
    ivy"org.openjfx:javafx-swing:${jfxVersion};classifier=${jfxClassifier}",
  )

  val jcvVersion = "1.5.5"
  val javacvClassifier = "linux-x86_64"
  val jcvDep = List(
    ivy"org.bytedeco:javacv:${jcvVersion}",
    ivy"org.bytedeco:ffmpeg:4.3.2-${jcvVersion};classifier=${javacvClassifier}",
    ivy"org.bytedeco:javacpp:${jcvVersion};classifier=${javacvClassifier}",
    ivy"org.bytedeco:openblas:0.3.13-${jcvVersion};classifier=${javacvClassifier}",
    ivy"org.bytedeco:opencv:4.5.1-${jcvVersion};classifier=${javacvClassifier}",
  )

  val djlVersion = "0.12.0"
  val djlDep = List(
    ivy"ai.djl:api:${djlVersion}",
    ivy"ai.djl:model-zoo:${djlVersion}",
    ivy"ai.djl.pytorch:pytorch-engine:${djlVersion}",
    ivy"ai.djl.pytorch:pytorch-model-zoo:${djlVersion}",
    ivy"ai.djl.pytorch:pytorch-native-cpu:1.8.1;classifier=linux-x86_64",
  )

  override def ivyDeps = Agg(
    ivy"com.novocode:junit-interface:0.11",
    ivy"org.scalafx:scalafx_3:16.0.0-R24",
    // json
    ivy"io.circe::circe-core:0.14.1",
    ivy"io.circe::circe-generic:0.14.1",
    ivy"io.circe::circe-parser:0.14.1",
    // log
    ivy"ch.qos.logback:logback-classic:1.2.3",
    ivy"com.typesafe.scala-logging::scala-logging:3.9.4",
    //mqtt
    ivy"com.lightbend.akka:akka-stream-alpakka-mqtt_2.13:3.0.2",
    // test
    ivy"org.scalactic::scalactic:3.2.9",
    ivy"org.scalatest::scalatest:3.2.9"
  ) ++ akkaDep ++ jfxDep ++ jcvDep ++ djlDep

  object test extends Tests with TestModule.Utest {
    override def ivyDeps = Agg(
      // test
      ivy"org.scalactic::scalactic:3.2.9",
      ivy"org.scalatest::scalatest:3.2.9"
    )
  }
}
